<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="java.util.*,support.*,java.sql.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Provide degrees - Choose university</title>
<script type="text/javascript" src="university.js"></script>
</head>
<body>
	<%
		
		Student student = (Student)session.getAttribute("student");
		Degree degree = (Degree)session.getAttribute("degree");
		ArrayList<Degree> degreeList = (ArrayList<Degree>)session.getAttribute("degreelist");
		degree.location(Integer.parseInt(request.getParameter("degreeLocation")));
		
	%>
	
	<h3>Previously entered in information:</h3>

	<p><%= "Full name: " + student.getFirstName() + " " + student.getMiddleName() + " " + student.getLastName() %></br>
	<%= "Country of Citizenship: " + student.getCitizenshipName() %></br>
	<% if (student.getState() != "") { %>
		<%= "Address: " + student.getAddress() + ", " + student.getCity() + " " + student.getState() + ", " + student.getZipcode() %>
	<% } else {%>
		<%= "Address: " + student.getAddress() + ", " + student.getCity() + ", " + student.getZipcode() %></p>
	<% } %>
	<p><b>Current Degree:</b><br>
	Location: <%= degree.getLocationName() %><br>
	</p>

	<%
   //Print out degree information if coming from more degrees
   if (degreeList != null) {
      for (int i = 0; i < degreeList.size(); i++) {
%>
         <p><b><%= "Degree #" + (i+1) + ": " %></b><br>
         <%= degreeList.get(i).getDisciplineName() + ", " + degreeList.get(i).getTitle() %><br> 
         <%= degreeList.get(i).getUniversityName() %><br>
         <%= degreeList.get(i).getLocationName() %><br>
         <%= "Year of Graduation: " + degreeList.get(i).getDate() %><br>
         <%= "GPA: " + degreeList.get(i).getGpa() %><br>
<%
	   }	   
   }
%>
	
	<!-- <form method="GET" action="discipline.jsp"> -->
	
  	<h3>Please select the university that awarded your degree:</h3>	

<table>
<% 
	Connection conn = null;
	PreparedStatement stm = null;
	ResultSet result = null;

	try {
	Class.forName("org.postgresql.Driver");
	conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Graduate_application_database", "postgres", "postgres");
	conn.setAutoCommit(false);
	stm = conn.prepareStatement("SELECT * FROM universities WHERE country_state_id = ? ");
	stm.setInt(1, degree.getLocation());
	result = stm.executeQuery();
	conn.commit();
	conn.setAutoCommit(true);
   
   for (int i = 0; result.next(); i++)
   {

	  // Begin row every 3 elements.
      if (i%3 == 0)
	  {
%>
         <tr>
<%    }   
%>
      <td><a href=discipline.jsp?university=<%=result.getInt("u_id")%>> <%=result.getString("university") %></a></td>

<%    // End row after 3rd element has been printed.
      if ((i+1)%3 == 0)
      {   
%>
         </tr>
<%    }
   }
   
	result.close();
	stm.close();
	conn.close();
}
catch (Exception e)
{
	throw new RuntimeException(e);
}
finally {
   if (result != null) {
       try {
           result.close();
       } 
       catch (SQLException e) { }
       result = null;
   }
   if (stm != null) {
       try {
           stm.close();
       } 
       catch (SQLException e) { }
       stm = null;
   }
   if (conn != null) {
       try {
           conn.close();
       } 
       catch (SQLException e) { }
       conn = null;
   }
}
%>
</table>

    <form method="GET" action="discipline.jsp" id="myform" >
	<p>If your university is not in this list, type it in here and press submit:</p>
		<input type = "text" name = "newuniversity" id="newuniversity" />
		<input type = "button" value="Submit" onClick = "validate();"/>
	</form>

</body>
</html>
